package com.jeesite.common.utils;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.jeesite.common.config.Global;
import com.jeesite.common.web.http.ServletUtils;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SmsUtils {
    private static final Logger logger = LoggerFactory.getLogger(SmsUtils.class);
    private static final int appId= Integer.parseInt(Global.getConfig("sms.appId"));
    private static final boolean enable= Boolean.parseBoolean(Global.getConfig("sms.enable"));
    private static final String appKey= Global.getConfig("sms.appKey");
    private static final String sign= Global.getConfig("sms.sign");
    private static  Map<String,Integer> templateMap;

    public static Integer getTemplate(String key){
        if(templateMap==null) {
            templateMap = new HashMap<String, Integer>();
            templateMap.put("register", Integer.parseInt(Global.getConfig("sms.template.register")));
            templateMap.put("forgotPwd", Integer.parseInt(Global.getConfig("sms.template.forgotPwd")));
            templateMap.put("apptConfirm", Integer.parseInt(Global.getConfig("sms.template.apptConfirm")));
            templateMap.put("apptCancel", Integer.parseInt(Global.getConfig("sms.template.apptCancel")));
            templateMap.put("apptChange", Integer.parseInt(Global.getConfig("sms.template.apptChange")));
        }
        return templateMap.get(key);
    }

    /**
     *
     * @param phone
     * @param templateId
     * @param params 数组具体的元素个数和模板中变量个数必须一致，例如事例中templateId:5678对应一个变量，参数数组中元素个数也必须是一个
     */
    public static String send(String phone, int templateId, String[] params){
    	if(!enable){
    		return ServletUtils.renderResult(Global.TRUE, "Not Enabled");
    	}
        String message;
        try {
            SmsSingleSender sender = new SmsSingleSender(appId, appKey);
            // 签名参数未提供或者为空时，会使用默认签名发送短信
            SmsSingleSenderResult result = sender.sendWithParam("86", phone, templateId, params, sign, "", "");
            return ServletUtils.renderResult(result.result==0 ? Global.TRUE:Global.FALSE, result.errMsg);
        } catch (com.github.qcloudsms.httpclient.HTTPException e) {
            message = "HTTP响应码错误";
            logger.error(message, e);
            return ServletUtils.renderResult(Global.FALSE, message);
        } catch (JSONException e) {
            message = "json解析错误";
            logger.error(message, e);
            return ServletUtils.renderResult(Global.FALSE, message);
        } catch (IOException e) {
            message = "网络错误";
            logger.error(message, e);
            return ServletUtils.renderResult(Global.FALSE, message);
        }
    }

    /* private static Map getResult(int code, String message){
        Map r = new HashMap();
        r.put("code", code);
        r.put("message", message);
        return r;
    }*/

    public static void main(String[] args) {
       send("13812558411", getTemplate("register"), new String[]{"5432"});
    }
}
